// /lib/js/graceChecker.js
const graceChecker = {
  error: '',
  check: function(data, rule) {
    this.error = ''; // 每次检查前清空错误信息
    
    for (let i = 0; i < rule.length; i++) {
      if (!rule[i].checkType) return true;
      if (!rule[i].name) return true;
      if (!rule[i].errorMsg) return true;
      
      // 检查字段是否存在
      if (data[rule[i].name] === undefined || data[rule[i].name] === null) {
        this.error = rule[i].errorMsg;
        return false;
      }

      switch (rule[i].checkType) {
        case 'string':
          const minMax = rule[i].checkRule.split(',');
          const min = parseInt(minMax[0]);
          const max = parseInt(minMax[1]);
          if (data[rule[i].name].length < min || data[rule[i].name].length > max) {
            this.error = rule[i].errorMsg;
            return false;
          }
          break;
          
        case 'in':
          const options = rule[i].checkRule.split(',');
          if (!options.includes(data[rule[i].name])) {
            this.error = rule[i].errorMsg;
            return false;
          }
          break;
          
        case 'notnull':
          if (!data[rule[i].name] || data[rule[i].name].length === 0) {
            this.error = rule[i].errorMsg;
            return false;
          }
          break;
          
        // 可以添加其他验证类型...
      }
    }
    return true;
  }
};

// 兼容ESM和CommonJS导出
export default graceChecker;
module.exports = graceChecker;